﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32;

namespace Meerkat_Service
{
    public class Config
    {
        public string[] EmailAddresses = new string[]{};
        public ArrayList DisksToMonitor = new ArrayList();
        public int ThresholdAmount;
        public string ThresholdUnit = "";
        public string SmtpServer = "";
        public string SmtpServerPort = "";
        public string SmtpServerUsername = "";
        public string SmtpServerPassword = "";
        public string SendFrom = "";
        public string MachineName = "";
        public bool LoadError;
        private string _RegistryRoot = @"Software\Kensei\Meerkat";
        public int EmailDelay = 45;

        private kensei.notify.WindowsEventLog notify;

        public Config() { notify = new kensei.notify.WindowsEventLog(); }

        public void LoadConfig()
        {
            RegistryKey key = Registry.LocalMachine.OpenSubKey(_RegistryRoot + @"\Disks");

            if (key != null)
            {
                string[] keys = key.GetValueNames();
                foreach (string k in key.GetValueNames())
                {
                    DisksToMonitor.Add(k);
                }
            }
            else
            {
                notify.WriteEvent("Meerkat", "Meerkat is incorrectly configured. Please run the Meerkat application. Service will remain idle until restarted.", kensei.notify.WindowsEventLog.LogEntryType.Error);
                LoadError = true;
                return;
            }

            if (DisksToMonitor.Count < 0)
            {
                notify.WriteEvent("Meerkat", "No disks were selected for monitoring. Service will remain idle until restarted.", kensei.notify.WindowsEventLog.LogEntryType.Warning);
                LoadError = true;
                return;
            }

            try
            {
                key = Registry.LocalMachine.OpenSubKey(_RegistryRoot + @"\Threshold");
                ThresholdAmount = Convert.ToInt32(key.GetValue("Amount", "1").ToString());
                ThresholdUnit = key.GetValue("Unit", "MB").ToString();

                key = Registry.LocalMachine.OpenSubKey(_RegistryRoot);
                if (string.IsNullOrEmpty(key.GetValue("EmailAddresses", "").ToString()))
                {
                    notify.WriteEvent("Meerkat", "No email addresses have been specified for alerts. Service will remain idle until restarted.", kensei.notify.WindowsEventLog.LogEntryType.Warning);
                    LoadError = true;
                    return;
                }

                if (key.GetValue("EmailAddresses", "").ToString().IndexOf(";") > 0)
                {
                    EmailAddresses = key.GetValue("EmailAddresses", "").ToString().Split(char.Parse(";"));
                }
                else
                {
                    EmailAddresses = new string[] { key.GetValue("EmailAddresses", "").ToString() };
                }

                if (string.IsNullOrEmpty(key.GetValue("MachineName", "").ToString()) || string.IsNullOrEmpty(key.GetValue("SmtpServer", "").ToString()) || string.IsNullOrEmpty(key.GetValue("SendFrom", "").ToString()))
                {
                    notify.WriteEvent("Meerkat", "Meerkat is incorrectly configured. Please run the Meerkat application. Service will remain idle until restarted.", kensei.notify.WindowsEventLog.LogEntryType.Error);
                    LoadError = true;
                    return;
                }

                MachineName = key.GetValue("MachineName", "").ToString();
                SmtpServer = key.GetValue("SmtpServer", "").ToString();
                SendFrom = key.GetValue("SendFrom", "").ToString();
                SmtpServerPort = key.GetValue("SmtpServerPort", "25").ToString();
                SmtpServerUsername = key.GetValue("SmtpServerUsername", "").ToString();
                SmtpServerPassword = key.GetValue("SmtpServerPassword", "").ToString();
                int.TryParse(key.GetValue("EmailDelay", "45").ToString(), out EmailDelay);
            }
            catch
            {
                notify.WriteEvent("Meerkat", "Meerkat is incorrectly configured. Please run the Meerkat application. Service will remain idle until restarted.", kensei.notify.WindowsEventLog.LogEntryType.Error);
                LoadError = true;
                return;
            }
        }
    }
}
